package com.tanhua.manage.controller;

import com.tanhua.manage.service.AnalysisByDayService;
import com.tanhua.manage.service.AnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/dashboard")
public class AnalysisController {

    @Autowired
    private AnalysisService analysisService;
    @Autowired
    private AnalysisByDayService analysisByDayService;

    /**
     * 接口名称：概要统计信息
     * 接口路径：GET/dashboard/summary
     */
    @GetMapping("/summary")
    public ResponseEntity<Object> summary() {
        return analysisService.summary();
    }

    /**
     * 接口名称：概要统计信息
     * 接口路径：GET/dashboard/summary
     * sd	是
     * 开始时间
     * <p>
     * ed	是
     * 结束时间
     * <p>
     * type	是
     * 101 新增 102 活跃用户 103 次日留存率
     */
    @GetMapping("/users")
    public ResponseEntity<Object> users(@RequestParam Map<String, Object> map) {
        String type = (String) map.get("type");
        String startDateStr = (String) map.get("sd");
        String endDateStr = (String) map.get("ed");

        Long i = Long.parseLong(endDateStr) - Long.parseLong(startDateStr);
        Long i2=1000 * 60 * 60 * 24 * 30L;
        if (i > i2) {
            return analysisByDayService.users(type, Long.parseLong(startDateStr), Long.valueOf(endDateStr));
        } else {
            return analysisService.users(Long.parseLong(startDateStr), Long.valueOf(endDateStr), Integer.valueOf(type));
        }
    }
}
